using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;

using TrueCode.Core.Aspects;

namespace EMan.Server
{
  public class EManServiceInstanceProvider : IInstanceProvider
  {
    private readonly Func< object > _factory;

    public EManServiceInstanceProvider( [NotNull] Func< object > factory )
    {
      if( factory == null ) throw new ArgumentNullException( "factory" );
      _factory = factory;

      Console.WriteLine( "EManServiceInstanceProvider created." );
    }

    public object GetInstance( InstanceContext instanceContext )
    {
      Console.WriteLine( "EManServiceInstanceProvider.GetInstance 1" );
      return _factory();
    }

    public object GetInstance( InstanceContext instanceContext, Message message )
    {
      Console.WriteLine( "EManServiceInstanceProvider.GetInstance 2" );
      return _factory();
    }

    public void ReleaseInstance( InstanceContext instanceContext, object instance ){}
  }
}